package com.chainyoung.component_tmperson.ui.activity.hdwallet;

import android.content.Intent;
import android.os.Handler;
import android.view.View;
import android.widget.AdapterView;
import android.widget.GridView;
import android.widget.TextView;

import com.chainyoung.common.appcommon.entity.HdWalletCache;
import com.chainyoung.common.base.BaseActivity;
import com.chainyoung.common.di.AppComponent;
import com.chainyoung.common.utils.SPUtil;
import com.chainyoung.common.utils.StringUtil;
import com.chainyoung.common.utils.ToastUtil;
import com.chainyoung.common.view.kprogresshud.KProgressHUD;
import com.chainyoung.component_tmperson.R;
import com.chainyoung.component_tmperson.R2;
import com.chainyoung.component_tmperson.ui.adapter.WordsAdapter;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Random;

import butterknife.BindView;
import butterknife.OnClick;


/**
 * 确认助记词保存过
 */
public class ConfirmSaveWordsActivity extends BaseActivity {


    @BindView(R2.id.tv_step)
    TextView tvStep;
    @BindView(R2.id.grid_view)
    GridView gridView;

    private int step;

    private List<String> wordsList = new ArrayList<>();
    private List<String> oriWords = new ArrayList<>();
    private WordsAdapter adapter;
    private String selectedWord = "";
    private int randomStep;
    private HdWalletCache hdWallet;

    @Override
    protected void setupActivityComponent(AppComponent appComponent) {

    }

    @Override
    protected void initUI() {
        setCurrentTitle("确认助记词");
        hdWallet = (HdWalletCache) getIntent().getSerializableExtra("hdWallet");
        step = getIntent().getIntExtra("step", 1);
        randomStep = new Random().nextInt(12);
        if (randomStep == 0) {
            randomStep = 1;
        }
        tvStep.setText("#" + randomStep);
        adapter = new WordsAdapter(mContext, wordsList);
        gridView.setAdapter(adapter);

        gridView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                adapter.setSelectIndex(position);
                adapter.notifyDataSetChanged();
                selectedWord = wordsList.get(position);
            }
        });

    }

    @Override
    protected void initData() {

        if (hdWallet != null) {
            oriWords.addAll(Arrays.asList(hdWallet.getMnemonic().split(" ")));
            wordsList.addAll(StringUtil.createRandomList(getRemainedList(oriWords, oriWords.get(randomStep - 1)), 5));
            wordsList.set(new Random().nextInt(5), oriWords.get(randomStep - 1));
            adapter.notifyDataSetChanged();
        }
    }

    @Override
    protected int getLayoutId() {
        return R.layout.activity_confirm_save_word;
    }


    @OnClick(R2.id.tv_next)
    void next() {
        if (selectedWord.equals(oriWords.get(randomStep - 1))) {
            if (step == 3) {
                //返回个人中心
                ToastUtil.showToast("备份成功");
                //launch(HdWalletSettingActivity.class);
                //SPUtil.put(LibConstant.BACKE_UP_MNEMONIC, true);
                SPUtil.put(hdWallet.getAddress(),true);
                finish();
            } else {
                Intent intent = new Intent(mContext, ConfirmSaveWordsActivity.class);
                intent.putExtra("step", step + 1);
                intent.putExtra("hdWallet", hdWallet);
                mContext.startActivity(intent);
                finish();
            }
        } else {
            KProgressHUD hd = KProgressHUD.create(this);
            hd.setCustomView(mContext, R.drawable.tips_error)
                    .setLabel(getString(R.string.tmperson_word_error))
                    .show().setDuration(1500);
            new Handler().postDelayed(new Runnable() {
                public void run() {
                    //execute the task
                    Intent intent = new Intent(mContext, PrepareBackUpWordsActivity.class);
                    intent.putExtra("hdwallet", hdWallet);
                    mContext.startActivity(intent);
                    finish();
                }
            }, 1500);

        }
    }

    public List<String> getRemainedList(List<String> list, String target) {
        List<String> remainedList = new ArrayList<>();
        for (String item : list) {
            if (!item.equals(target)) {
                remainedList.add(item);
            }
        }

        return remainedList;
    }


}
